<?php
$ColArray;
function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
    return;
}

function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}

function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
    return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
    $L = strlen($Value);

    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
return;
}
function chkCol($cval)
{
    for ($a=0; $a<=count($ColArray)-1;$a++)
    {
        if ($cval==$ColArray[$a])
        {
            return $a;
            break;
        }

    }
    return -1;
}



    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Disposition: attachment;filename=Graph_Data.xls ");
    header("Content-Transfer-Encoding: binary ");

    // XLS Data Cell

    if ($tag=="BIO")
    {
         xlsBOF();
$ColArray=array();
                xlsWriteLabel(0,0,"X-AXIS");

                $xlsRow = 1;
                $xlsCol=1;
                foreach($data as $d)
                {//xlsWriteLabel($xlsRow,0,$d);
                $colData=explode("@@",$d);
                foreach($colData as $cd)
                {
                $a=explode(":",$cd);
                if (count($a)>1){
                 $tmp=chkCol($a[0]);

                 xlsWriteNumber($xlsRow,$xlsCol,$a[2]);
                 xlsWriteLabel(0,$xlsCol,$a[0]);
                 if ($tmp==-1)
                 {
                     xlsWriteLabel($xlsRow,0,$a[1]);
                 }
                 else
                 {
                     xlsWriteLabel($tmp,0,$a[1]);
                 }

                 $ColArray[$xlsRow]=$a[1];
                $xlsRow++;
                }
                }
                $xlsCol++;
                }
//        xlsBOF();
//$ColArray=array();
//                xlsWriteLabel(0,0,"X-AXIS");
//
//                $xlsRow = 1;
//                $xlsCol=1;
//                foreach($data as $d)
//                {//xlsWriteLabel($xlsRow,0,$d);
//
//                $a=explode(":",$d);
//
//                 $tmp=chkCol($a[0]);
//
//                 xlsWriteNumber($xlsRow,$xlsCol,$a[2]);
//                 xlsWriteLabel(0,$xlsCol,$a[0]);
//                 if ($tmp==-1)
//                 {
//                     xlsWriteLabel($xlsRow,0,$a[1]);
//                 }
//                 else
//                 {
//                     xlsWriteLabel($tmp,0,$a[1]);
//                 }
//
//                 $ColArray[$xlsRow]=$a[1];
//                $xlsRow++;
//                $xlsCol++;
//                }



    }
    else if ($tag=="NONBIO")
    {
        xlsBOF();
$ColArray=array();
                xlsWriteLabel(0,0,"X-AXIS");

                $xlsRow = 1;
                $xlsCol=1;
                foreach($data as $d)
                {//xlsWriteLabel($xlsRow,0,$d);
                $colData=explode("@@",$d);
                foreach($colData as $cd)
                {
                $a=explode(":",$cd);
                if (count($a)>1){
                 $tmp=chkCol($a[0]);

                 xlsWriteNumber($xlsRow,$xlsCol,$a[2]);
                 xlsWriteLabel(0,$xlsCol,$a[0]);
                 if ($tmp==-1)
                 {
                     xlsWriteLabel($xlsRow,0,$a[1]);
                 }
                 else
                 {
                     xlsWriteLabel($tmp,0,$a[1]);
                 }

                 $ColArray[$xlsRow]=$a[1];
                $xlsRow++;
                }
                }
                $xlsCol++;
                }
    }
                
                     xlsEOF();
                 exit();
?>